package com.vf.admin.client.service.impl;

import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.DynamicAlertDao;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.DynamicAlertService;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.entity.DynamicAlert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DynamicAlertServiceImpl implements DynamicAlertService {

    @Autowired
    private DynamicAlertDao dynamicAlertDao;
    @Autowired
    private AppUserService appUserService;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<DynamicAlert> page(Integer userId, SplitPageBean splitPage) {
        return dynamicAlertDao.page(userId, splitPage);
    }

    @Override
    public void add(Integer fromUserId, Integer userId, Integer dynamicId, String dynamicImage, Integer type, String content) {
        //给发布人增加动态通知
        redisUtil.set(String.format(RedisKeys.USER_BADGE, userId), 1);

        AppUser fromUser = appUserService.get(fromUserId);
        AppUser user = appUserService.get(userId);
        DynamicAlert dynamicAlert = new DynamicAlert();
        dynamicAlert.setFromUserId(fromUserId);
        dynamicAlert.setFromUserNick(fromUser.getNick());
        dynamicAlert.setFromUserPic(fromUser.getPic());
        dynamicAlert.setUserId(userId);
        dynamicAlert.setNick(user.getNick());
        dynamicAlert.setPic(user.getPic());
        dynamicAlert.setDynamicId(dynamicId);
        dynamicAlert.setDynamicImage(dynamicImage);
        dynamicAlert.setType(type);
        dynamicAlert.setContent(content);
        dynamicAlert.setCreateTime(new Date());
        dynamicAlertDao.add(dynamicAlert);
    }

    /**
     * 删除点赞的动态
     *
     * @param fromUserId
     * @param userId
     * @param dynamicId
     */
    @Override
    public void delParseDynamic(Integer fromUserId, Integer userId, Integer dynamicId) {
        dynamicAlertDao.delParseDynamic(fromUserId, userId, dynamicId);
    }

    /**
     * 删除点赞的评论
     *
     * @param fromUserId
     * @param userId
     * @param dynamicId
     */
    @Override
    public void delParseComment(Integer fromUserId, Integer userId, Integer dynamicId) {
        dynamicAlertDao.delParseComment(fromUserId, userId, dynamicId);
    }
}
